import 'package:flutter/material.dart';
import 'global/theme.dart';
import 'detail.dart';
import 'component.dart';
import 'datascreen.dart';
import 'rankstatistics.dart';
import 'global/event_bus.dart';

void main() => runApp(WGViewer());


class WGViewer extends StatefulWidget {
  @override
  _WGViewerState createState() {
    return _WGViewerState();
  }
}

class _WGViewerState extends State<WGViewer>
    with SingleTickerProviderStateMixin {
  TabController _tabController;
  int currentIdx = 0;
  String nowDate = '';
  @override
  void initState() {
    super.initState();
    bus.on('nowdate', changeTime);
    _tabController = TabController(length: 3, vsync: this);
    _tabController.addListener((){
      setState(() {
        currentIdx = _tabController.index;
      });
    });
  }

  TabBarView makeContentView() {
    return TabBarView(
        controller: _tabController,
        children: [
          DataScreen(),
          Platform(),
          RankViewer()
        ].toList());
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'charts',
      debugShowCheckedModeBanner: false,
      home: Scaffold(
          body: Container(
              color: BackColor,
              child: SafeArea(
                child: Container(
                  color: BackColor,
                  child: Column(
                    children: <Widget>[
                      SwitchBar(
                        title: nowDate,
                        onClick: (idx){
                          _tabController.animateTo(idx);
                        },
                        currentIndex: currentIdx,
                      ),
                      Expanded(
                          child: makeContentView(),
                      )
                    ],
                  ),
                ),
              ))),
    );
  }
  void changeTime(time){
    this.setState((){
      nowDate = time;
    });
  }
  @override
  void dispose(){
    _tabController.dispose();
    bus.off('nowdate', changeTime);
    super.dispose();
  }
}